package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    /**
     * 分页查询护理等级信息
     * @return
     */
    @Override
    public PageBean<NursingLevelVo> pageBean(NursingLevelPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<NursingLevel> page = nursingLevelMapper.pageBean(dto);
        return PageBean.of(page, NursingLevelVo.class);
    }
    /**
     * 插入护理等级信息
     * @param dto
     * @return
     */
    @Override
    public void insert(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.insert(nursingLevel);
    }
    /**
     * 根据ID查询护理等级信息
     */
    @Override
    public NursingLevelVo getById(Long id) {
        return nursingLevelMapper.getById(id);
    }

    /**
     * 更新护理等级信息
     * @param dto
     */
    @Override
    public void update(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.update(nursingLevel);
    }
    /**
     * 删除护理等级信息
     */
    @Override
    public void delete(Long id) {
        if(nursingLevelMapper.getById(id).getStatus()==0){
            nursingLevelMapper.delete(id);
        }else {
            throw new BeanException("该护理等级正在启用,无法删除!");
        }
    }
    /**
     * 启用、禁用护理等级
     */
    @Override
    public void startOrStop(Long id, Integer status) {
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setId(id);
        nursingLevel.setStatus(status);
        nursingLevelMapper.update(nursingLevel);
    }
    /**
     * 查询所有护理等级信息
     */
    @Override
    public List<NursingLevelVo> getAll() {
        return nursingLevelMapper.getAll();
    }
}


